package com.fowo.api.controller.purchase;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.PurchaseOrder;
import com.fowo.api.entity.PurchaseOrderProduct;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderBatchApprovalAction;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderVo;
import com.fowo.api.model.purchase.rgoods.order.product.PurchaseRgoodsOrderProductItemVo;
import com.fowo.api.model.stock.out.StockOutConfirmOutboundBatchAction;
import com.fowo.api.service.PurchaseRgoodsOrderProductService;
import com.fowo.api.service.impl.PurchaseOrderProductServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderServiceImpl;
import com.fowo.api.service.impl.PurchaseRgoodsOrderServiceImpl;
import com.fowo.api.service.impl.StockOutServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class PurchaseRgoodsOrderBatchApproval implements PurchaseRgoodsOrderBatchApprovalAction {
    @Resource
    private StockOutServiceImpl stockOutService;
    @Resource
    private PurchaseRgoodsOrderServiceImpl purchaseRgoodsOrderService;
    @Resource
    protected PurchaseRgoodsOrderProductService purchaseRgoodsOrderProductService;
    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    private PurchaseOrderProductServiceImpl purchaseOrderProductService;
    @Autowired(required = false)
    protected StockOutConfirmOutboundBatchAction  stockOutConfirmOutboundBatchAction;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            Long aLong = idList.get(i);
            handle(aLong);
        }

    }

    void  handle(Long id) throws Exception {
        //获取退货单
        PurchaseRgoodsOrderVo voById = purchaseRgoodsOrderService.getVoById(id);
        Assert.notNull(voById, "退货单不存在!");
        Assert.notNull(voById.getStatus().equals("0"), "违规操作!");
        StockOutVo model = new StockOutVo();
        model.setWarehouse(voById.getWid());
        model.setProductType("1");//先默认成品的CS01
        model.setType("CK004");
        model.setAStatus("40");
        model.setStatus("CS01");
        model.setOutTime(new Date());
        model.setRemark(voById.getRemark());
        List<StockOutProductItemVo> stockOutProducts = new ArrayList<>();
        for (int i = 0; i < voById.getProducts().size(); i++) {
            PurchaseRgoodsOrderProductItemVo purchaseRgoodsOrderProductItemVo = voById.getProducts().get(i);
//            PurchaseOrderProduct purchaseOrderProduct = purchaseOrderProductService.getById(childrenDTO.getFromMap().getId());
            StockOutProductItemVo stockOutProductItemVo = new StockOutProductItemVo();
            stockOutProductItemVo.setProductId(purchaseRgoodsOrderProductItemVo.getProductId());
            stockOutProductItemVo.setSku(purchaseRgoodsOrderProductItemVo.getProductSku());
            stockOutProductItemVo.setProductGoodNum(purchaseRgoodsOrderProductItemVo.getQuantity());
            stockOutProductItemVo.setSid(purchaseRgoodsOrderProductItemVo.getSid());
            stockOutProductItemVo.setFnsku(purchaseRgoodsOrderProductItemVo.getFnskuName() != null ? purchaseRgoodsOrderProductItemVo.getFnskuName() : null);
            stockOutProductItemVo.setPrice(purchaseRgoodsOrderProductItemVo.getPrice());
            stockOutProductItemVo.setProductBadNum(purchaseRgoodsOrderProductItemVo.getReturnBadNum());
//            stockOutProductItemVo.setProductGoodNum(purchaseRgoodsOrderProductItemVo.getQuantity());
//            BigDecimal unitPrice=purchaseOrderProduct.getUnitPrice()==null?new BigDecimal("0"):purchaseOrderProduct.getUnitPrice();
//            Long GoodNum=childrenDTO.getToMap().getQuantity()==null?0:childrenDTO.getToMap().getQuantity();
            stockOutProductItemVo.setQuantity(purchaseRgoodsOrderProductItemVo.getQuantity());
            BigDecimal unitPrice=purchaseRgoodsOrderProductItemVo.getPrice()==null?new BigDecimal("0"):purchaseRgoodsOrderProductItemVo.getPrice();
            stockOutProductItemVo.setSubtotal(new BigDecimal(stockOutProductItemVo.getQuantity()).multiply(unitPrice));
            stockOutProducts.add(stockOutProductItemVo);
        }
        model.setRId(voById.getId());//设置退货单的id
        model.setStockOutProducts(stockOutProducts);
        Long aLong = stockOutService.create(model);
        stockOutConfirmOutboundBatchAction.run(Arrays.asList(aLong));
//        stockOutService.outStockApply(aLong);
        //更新采购单退款数量
        //审核出库单
        //退货扣款
        if (voById.getReturnMethod().equals("1")) {
            for (int i = 0; i < voById.getProducts().size(); i++) {
                PurchaseRgoodsOrderProductItemVo purchaseRgoodsOrderProductItemVo = voById.getProducts().get(i);
                PurchaseOrderProduct purchaseOrderProduct = purchaseOrderProductService.getById(purchaseRgoodsOrderProductItemVo.getPurchaseOrderProductId());
                if(purchaseOrderProduct.getBusinessIdent()==null){
                    purchaseOrderProduct.setBusinessIdent("1");
                }else{
                    if(!purchaseOrderProduct.getBusinessIdent().contains("1")){
                        purchaseOrderProduct.setBusinessIdent(purchaseOrderProduct.getBusinessIdent()+",1");
                    }
                }
               Assert.isTrue(purchaseOrderProduct.getCrossChecking()>=purchaseRgoodsOrderProductItemVo.getQuantity(),"到货数量小于退货数量");
                purchaseOrderProduct.setCrossChecking(purchaseOrderProduct.getCrossChecking()-purchaseRgoodsOrderProductItemVo.getQuantity());
                purchaseOrderProduct.setQuantityReturn(purchaseOrderProduct.getQuantityReturn()==null?purchaseRgoodsOrderProductItemVo.getQuantity():purchaseOrderProduct.getQuantityReturn()+purchaseRgoodsOrderProductItemVo.getQuantity());
                purchaseOrderProductService.updateById(purchaseOrderProduct);
            }
        }
        //退货补货
        else if (voById.getReturnMethod() != null && voById.getReturnMethod().equals("2")) {
            //退货补货
            PurchaseOrder one = purchaseOrderService.getOne(Wrappers.<PurchaseOrder>query().lambda().eq(PurchaseOrder::getPurchaseOrderCode, voById.getPurchaseOrderCode()));

            for (int i = 0; i < voById.getProducts().size(); i++) {
                PurchaseRgoodsOrderProductItemVo purchaseRgoodsOrderProductItemVo = voById.getProducts().get(i);
                PurchaseOrderProduct purchaseOrderProduct = purchaseOrderProductService.getById(purchaseRgoodsOrderProductItemVo.getPurchaseOrderProductId());
                purchaseOrderProduct.setQuantityReceive(purchaseOrderProduct.getQuantityReceive() + purchaseRgoodsOrderProductItemVo.getQuantity().intValue());
                if(purchaseOrderProduct.getBusinessIdent()==null){
                    purchaseOrderProduct.setBusinessIdent("2");
                }else{
                    if(!purchaseOrderProduct.getBusinessIdent().contains("2")){
                        purchaseOrderProduct.setBusinessIdent(purchaseOrderProduct.getBusinessIdent()+",2");
                    }
                }
                Assert.isTrue(purchaseOrderProduct.getCrossChecking()>=purchaseRgoodsOrderProductItemVo.getQuantity(),"到货数量小于退货数量");
                purchaseOrderProduct.setCrossChecking(purchaseOrderProduct.getCrossChecking()-purchaseRgoodsOrderProductItemVo.getQuantity());
                purchaseOrderProduct.setQuantityReturn(purchaseOrderProduct.getQuantityReturn()==null?purchaseRgoodsOrderProductItemVo.getQuantity():purchaseOrderProduct.getQuantityReturn()+purchaseRgoodsOrderProductItemVo.getQuantity());
                purchaseOrderProductService.updateById(purchaseOrderProduct);
            }
            one.setAnStatus("2");
            one.setArrivalStatus("1");
            purchaseOrderService.updateById(one);

        }
        voById.setStatus("3");
        purchaseRgoodsOrderService.update(voById);
    }
}
